const formatTime = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return `${[year, month, day].map(formatNumber).join("/")} ${[
    hour,
    minute,
    second,
  ]
    .map(formatNumber)
    .join(":")}`;
};

const formatNumber = (n) => {
  n = n.toString();
  return n[1] ? n : `0${n}`;
};

const validatePhoneNumber = (phoneNumber) => {
  var regex = /^1[23456789]\d{9}$/;
  return regex.test(phoneNumber);
};

const validateIDCard = (idCard) => {
  var regex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  return regex.test(idCard);
};
// 自定义方法：从 URL 中获取指定参数
const getQueryString = (url, param) => {
  console.log(url, param);

  const queryString = url.split("?")[1] || ""; // 获取 ? 后面的查询字符串
  const params = queryString.split("&"); // 分割查询参数
  for (let i = 0; i < params.length; i++) {
    const pair = params[i].split("=");
    if (pair[0] === param) {
      return decodeURIComponent(pair[1] || "");
    }
  }
  return null; // 如果未找到参数，则返回 null
};

module.exports = {
  formatTime,
  validatePhoneNumber,
  validateIDCard,
  getQueryString,
};
